from subprocess import Popen, PIPE
import sys
from interval import setInterval

def execute_espeak(cmd):
    cmd_b = 'espeak -vzh+m2 -s180 -k5 '
    cmd_e = ' 2>>/dev/null'
    p = Popen(cmd_b + cmd + cmd_e, stdout=PIPE, shell=True)
    (output, _) = p.communicate()
    return output

def repeatPlayEspeak(text):

    @setInterval(.3)
    def loop():
        execute_espeak(text)

    stopped = loop()
    return stopped

if __name__ == '__main__':
    if len(sys.argv) > 1:
        t = sys.argv[1]
    else:
        t = "中文语音"

    stop = repeatPlayEspeak(t)
    sys.stdin.read(1)
    stop.set()
# while (True):
#     execute_espeak(t)

